


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html id="htmlId">
<head>
  <title>Coverage Report :: ViennaUtils</title>
  <style type="text/css">
    @import "../../.css/coverage.css";
  </style>
</head>

<body>
<div class="header"></div>

<div class="content">
<div class="breadCrumbs">
    [ <a href="../../index.html">all classes</a> ]
    [ <a href="../index.html">at.schauer.gregor.vienna.util</a> ]
</div>

<h1>Coverage Summary for Class: ViennaUtils (at.schauer.gregor.vienna.util)</h1>

<table class="coverageStats">

<tr>
  <th class="name">Class</th>
<th class="coverageStat 
">
  Method, %
</th>
<th class="coverageStat 
">
  Line, %
</th>
</tr>
<tr>
  <td class="name">ViennaUtils</td>
<td class="coverageStat">
  <span class="percent">
    50%
  </span>
  <span class="absValue">
    (1/ 2)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    80%
  </span>
  <span class="absValue">
    (8/ 10)
  </span>
</td>
</tr>
  <tr>
    <td class="name">ViennaUtils$ConverterUtils</td>
<td class="coverageStat">
  <span class="percent">
    71.4%
  </span>
  <span class="absValue">
    (5/ 7)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    56%
  </span>
  <span class="absValue">
    (14/ 25)
  </span>
</td>
  </tr>
  <tr>
    <td class="name">ViennaUtils$DeserializationUtils</td>
<td class="coverageStat">
  <span class="percent">
    50%
  </span>
  <span class="absValue">
    (2/ 4)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    29.4%
  </span>
  <span class="absValue">
    (5/ 17)
  </span>
</td>
  </tr>
  <tr>
    <td class="name">ViennaUtils$SerializationUtils</td>
<td class="coverageStat">
  <span class="percent">
    50%
  </span>
  <span class="absValue">
    (2/ 4)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    54.5%
  </span>
  <span class="absValue">
    (6/ 11)
  </span>
</td>
  </tr>
  <tr>
    <td class="name">ViennaUtils$TypeUtils</td>
<td class="coverageStat">
  <span class="percent">
    66.7%
  </span>
  <span class="absValue">
    (2/ 3)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    91.7%
  </span>
  <span class="absValue">
    (22/ 24)
  </span>
</td>
  </tr>
<tr>
  <td class="name"><strong>total</strong></td>
<td class="coverageStat">
  <span class="percent">
    60%
  </span>
  <span class="absValue">
    (12/ 20)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    63.2%
  </span>
  <span class="absValue">
    (55/ 87)
  </span>
</td>
</tr>
</table>

<br/>
<br/>


<div class="sourceCode"><i>1</i>&nbsp;/*
<i>2</i>&nbsp; * Copyright 2012 Gregor Schauer
<i>3</i>&nbsp; *
<i>4</i>&nbsp; * Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<i>5</i>&nbsp; * you may not use this file except in compliance with the License.
<i>6</i>&nbsp; * You may obtain a copy of the License at
<i>7</i>&nbsp; *
<i>8</i>&nbsp; *      http://www.apache.org/licenses/LICENSE-2.0
<i>9</i>&nbsp; *
<i>10</i>&nbsp; * Unless required by applicable law or agreed to in writing, software
<i>11</i>&nbsp; * distributed under the License is distributed on an &quot;AS IS&quot; BASIS,
<i>12</i>&nbsp; * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<i>13</i>&nbsp; * See the License for the specific language governing permissions and
<i>14</i>&nbsp; * limitations under the License.
<i>15</i>&nbsp; */
<i>16</i>&nbsp;package at.schauer.gregor.vienna.util;
<i>17</i>&nbsp;
<i>18</i>&nbsp;import at.schauer.gregor.vienna.context.ViennaContext;
<i>19</i>&nbsp;import at.schauer.gregor.vienna.convert.provider.ConverterProvider;
<i>20</i>&nbsp;import at.schauer.gregor.vienna.datastore.DataStore;
<i>21</i>&nbsp;import at.schauer.gregor.vienna.exception.ViennaException;
<i>22</i>&nbsp;import at.schauer.gregor.vienna.metadata.MetadataProvider;
<i>23</i>&nbsp;import at.schauer.gregor.vienna.serializer.Serializer;
<i>24</i>&nbsp;import org.apache.commons.lang3.ObjectUtils;
<i>25</i>&nbsp;import org.springframework.util.ReflectionUtils;
<i>26</i>&nbsp;import sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl;
<i>27</i>&nbsp;
<i>28</i>&nbsp;import javax.annotation.Nonnull;
<i>29</i>&nbsp;import javax.annotation.Nullable;
<i>30</i>&nbsp;import java.lang.reflect.Field;
<i>31</i>&nbsp;import java.lang.reflect.Type;
<i>32</i>&nbsp;import java.util.LinkedHashMap;
<i>33</i>&nbsp;import java.util.Map;
<i>34</i>&nbsp;
<i>35</i>&nbsp;import static at.schauer.gregor.vienna.util.ViennaUtils.ConverterUtils.convert;
<i>36</i>&nbsp;import static at.schauer.gregor.vienna.util.ViennaUtils.ConverterUtils.hasConverter;
<i>37</i>&nbsp;
<i>38</i>&nbsp;/**
<i>39</i>&nbsp; * @author Gregor Schauer
<i>40</i>&nbsp; */
<i>41</i>&nbsp;public class ViennaUtils {
<b class="nc"><i>42</i>&nbsp;	private ViennaUtils() {</b>
<b class="nc"><i>43</i>&nbsp;	}</b>
<i>44</i>&nbsp;
<i>45</i>&nbsp;	public static &lt;X&gt; X filter(Map&lt;Class&lt;?&gt;, X&gt; map, Class clazz) {
<b class="fc"><i>46</i>&nbsp;		X elem = map.get(clazz);</b>
<b class="fc"><i>47</i>&nbsp;		if (elem == null) {</b>
<b class="fc"><i>48</i>&nbsp;			for (Map.Entry&lt;Class&lt;?&gt;, X&gt; entry : map.entrySet()) {</b>
<b class="fc"><i>49</i>&nbsp;				if (entry.getKey().isAssignableFrom(clazz)) {</b>
<b class="fc"><i>50</i>&nbsp;					elem = entry.getValue();</b>
<b class="fc"><i>51</i>&nbsp;					map.put(clazz, elem);</b>
<b class="fc"><i>52</i>&nbsp;					break;</b>
<i>53</i>&nbsp;				}
<i>54</i>&nbsp;			}
<i>55</i>&nbsp;		}
<b class="fc"><i>56</i>&nbsp;		return elem;</b>
<i>57</i>&nbsp;	}
<i>58</i>&nbsp;
<i>59</i>&nbsp;
<i>60</i>&nbsp;	public static class TypeUtils {
<b class="nc"><i>61</i>&nbsp;		private TypeUtils() {</b>
<b class="nc"><i>62</i>&nbsp;		}</b>
<i>63</i>&nbsp;
<i>64</i>&nbsp;		@Nonnull
<i>65</i>&nbsp;		public static Map&lt;Type, Class&gt; getGenericTypes(@Nonnull Class clazz) {
<b class="fc"><i>66</i>&nbsp;			Map&lt;Type, Class&gt; map = new LinkedHashMap&lt;Type, Class&gt;();</b>
<b class="fc"><i>67</i>&nbsp;			Type[] genericInterfaces = clazz.getGenericInterfaces();</b>
<b class="fc"><i>68</i>&nbsp;			for (Type type : genericInterfaces) {</b>
<b class="fc"><i>69</i>&nbsp;				if (!ParameterizedTypeImpl.class.isAssignableFrom(type.getClass())) {</b>
<b class="fc"><i>70</i>&nbsp;					type = clazz.getGenericSuperclass();</b>
<i>71</i>&nbsp;				}
<b class="fc"><i>72</i>&nbsp;				Class bar = getGenericTypeArgument(type);</b>
<b class="fc"><i>73</i>&nbsp;				if (bar != null) {</b>
<b class="fc"><i>74</i>&nbsp;					map.put(type, bar);</b>
<i>75</i>&nbsp;				} else {
<b class="fc"><i>76</i>&nbsp;					map.putAll(getGenericTypes(clazz.getSuperclass()));</b>
<i>77</i>&nbsp;				}
<i>78</i>&nbsp;			}
<b class="fc"><i>79</i>&nbsp;			if (genericInterfaces.length == 0) {</b>
<b class="fc"><i>80</i>&nbsp;				Type type = clazz.getGenericSuperclass();</b>
<b class="fc"><i>81</i>&nbsp;				if (type != null) {</b>
<b class="fc"><i>82</i>&nbsp;					map.put(type, getGenericTypeArgument(type));</b>
<i>83</i>&nbsp;				}
<i>84</i>&nbsp;			}
<b class="fc"><i>85</i>&nbsp;			return map;</b>
<i>86</i>&nbsp;		}
<i>87</i>&nbsp;
<i>88</i>&nbsp;		@Nullable
<i>89</i>&nbsp;		public static Class getGenericTypeArgument(@Nullable Type type) {
<b class="fc"><i>90</i>&nbsp;			if (type != null &amp;&amp; ParameterizedTypeImpl.class.isAssignableFrom(type.getClass())) {</b>
<b class="fc"><i>91</i>&nbsp;				ParameterizedTypeImpl parameterizedType = ParameterizedTypeImpl.class.cast(type);</b>
<b class="fc"><i>92</i>&nbsp;				for (Type actualTypeArgument : parameterizedType.getActualTypeArguments()) {</b>
<b class="fc"><i>93</i>&nbsp;					if (actualTypeArgument.getClass() == Class.class) {</b>
<b class="fc"><i>94</i>&nbsp;						return Class.class.cast(actualTypeArgument);</b>
<b class="fc"><i>95</i>&nbsp;					} else if (ParameterizedTypeImpl.class.isAssignableFrom(actualTypeArgument.getClass())) {</b>
<b class="fc"><i>96</i>&nbsp;						return ParameterizedTypeImpl.class.cast(actualTypeArgument).getRawType();</b>
<i>97</i>&nbsp;					}
<i>98</i>&nbsp;				}
<i>99</i>&nbsp;			}
<b class="fc"><i>100</i>&nbsp;			return null;</b>
<i>101</i>&nbsp;		}
<i>102</i>&nbsp;	}
<i>103</i>&nbsp;
<i>104</i>&nbsp;
<i>105</i>&nbsp;	public static class ConverterUtils {
<b class="nc"><i>106</i>&nbsp;		private ConverterUtils() {</b>
<b class="nc"><i>107</i>&nbsp;		}</b>
<i>108</i>&nbsp;
<i>109</i>&nbsp;		@SuppressWarnings(&quot;unchecked&quot;)
<i>110</i>&nbsp;		protected static &lt;T&gt; T convert(ViennaContext ctx, Object obj, Class&lt;T&gt; targetClass) {
<b class="fc"><i>111</i>&nbsp;			if (obj.getClass() == targetClass) {</b>
<b class="fc"><i>112</i>&nbsp;				return (T) obj;</b>
<i>113</i>&nbsp;			}
<i>114</i>&nbsp;
<i>115</i>&nbsp;			try {
<b class="fc"><i>116</i>&nbsp;				ConverterProvider&lt;?&gt; converterProvider = ctx.getConverterProvider(obj.getClass());</b>
<b class="fc"><i>117</i>&nbsp;				return converterProvider.convert(obj, targetClass);</b>
<b class="nc"><i>118</i>&nbsp;			} catch (Exception e) {</b>
<b class="nc"><i>119</i>&nbsp;				throw new ViennaException(&quot;Cannot convert &#39;&quot; + obj + &quot;&#39; to &#39;&quot; + targetClass + &#39;\&#39;&#39;, e);</b>
<i>120</i>&nbsp;			}
<i>121</i>&nbsp;		}
<i>122</i>&nbsp;
<i>123</i>&nbsp;		public static boolean hasConverter(ViennaContext ctx, Class&lt;?&gt; sourceClass, Class&lt;?&gt; targetClass) {
<i>124</i>&nbsp;			try {
<b class="fc"><i>125</i>&nbsp;				ConverterProvider&lt;?&gt; converterProvider = ctx.getConverterProvider(sourceClass);</b>
<b class="fc"><i>126</i>&nbsp;				Object converter = converterProvider.getConverter(sourceClass, targetClass);</b>
<b class="fc"><i>127</i>&nbsp;				if (converter != null) {</b>
<b class="fc"><i>128</i>&nbsp;					return true;</b>
<i>129</i>&nbsp;				}
<b class="nc"><i>130</i>&nbsp;			} catch (Exception e) {</b>
<i>131</i>&nbsp;				// ignore
<b class="nc"><i>132</i>&nbsp;			}</b>
<b class="nc"><i>133</i>&nbsp;			return sourceClass == targetClass;</b>
<i>134</i>&nbsp;		}
<i>135</i>&nbsp;
<i>136</i>&nbsp;		@SuppressWarnings(&quot;unchecked&quot;)
<i>137</i>&nbsp;		public static String typeName(ViennaContext ctx, Class clazz) {
<b class="nc"><i>138</i>&nbsp;			if (clazz == null) {</b>
<b class="nc"><i>139</i>&nbsp;				return ObjectUtils.Null.class.getSimpleName();</b>
<i>140</i>&nbsp;			}
<b class="nc"><i>141</i>&nbsp;			MetadataProvider infoProvider = ctx.getMetadataProvider(clazz);</b>
<b class="nc"><i>142</i>&nbsp;			return infoProvider != null ? infoProvider.getEntityName(clazz) : clazz.getSimpleName();</b>
<i>143</i>&nbsp;		}
<i>144</i>&nbsp;
<i>145</i>&nbsp;		public static String type(Class clazz) {
<b class="fc"><i>146</i>&nbsp;			return (clazz != null ? clazz : ObjectUtils.Null.class).getName();</b>
<i>147</i>&nbsp;		}
<i>148</i>&nbsp;
<i>149</i>&nbsp;		public static String typeFromString(String str) {
<b class="fc"><i>150</i>&nbsp;			int posSep = str.indexOf(Serializer.CLASS_OBJECT_SEPARATOR);</b>
<b class="fc"><i>151</i>&nbsp;			int posEq = str.indexOf(&#39;=&#39;) + 1;</b>
<b class="fc"><i>152</i>&nbsp;			return posSep &gt; 0 ? posEq &gt; posSep ? str.substring(0, posSep) : str.substring(posEq, posSep) : str;</b>
<i>153</i>&nbsp;		}
<i>154</i>&nbsp;
<i>155</i>&nbsp;		public static String valueFromString(String str) {
<b class="fc"><i>156</i>&nbsp;			int pos = str.indexOf(Serializer.CLASS_OBJECT_SEPARATOR) + Serializer.CLASS_OBJECT_SEPARATOR.length();</b>
<b class="fc"><i>157</i>&nbsp;			return pos &gt; 0 ? str.substring(pos) : str;</b>
<i>158</i>&nbsp;		}
<i>159</i>&nbsp;	}
<i>160</i>&nbsp;
<i>161</i>&nbsp;
<i>162</i>&nbsp;	public static class SerializationUtils {
<b class="nc"><i>163</i>&nbsp;		private SerializationUtils() {</b>
<b class="nc"><i>164</i>&nbsp;		}</b>
<i>165</i>&nbsp;
<i>166</i>&nbsp;		@SuppressWarnings(&quot;unchecked&quot;)
<i>167</i>&nbsp;		public static &lt;T&gt; String save(ViennaContext ctx, T obj) {
<b class="fc"><i>168</i>&nbsp;			if (obj == null) {</b>
<b class="fc"><i>169</i>&nbsp;				return String.valueOf(obj);</b>
<i>170</i>&nbsp;			}
<i>171</i>&nbsp;
<b class="fc"><i>172</i>&nbsp;			boolean hasConverter = hasConverter(ctx, obj.getClass(), String.class);</b>
<b class="fc"><i>173</i>&nbsp;			if (hasConverter) {</b>
<b class="fc"><i>174</i>&nbsp;				return saveViaConverter(ctx, obj);</b>
<i>175</i>&nbsp;			} else {
<b class="nc"><i>176</i>&nbsp;				DataStore&lt;T&gt; dataStore = ctx.getDataStore((Class&lt;T&gt;) obj.getClass());</b>
<b class="nc"><i>177</i>&nbsp;				return saveViaDataStore(dataStore, obj);</b>
<i>178</i>&nbsp;			}
<i>179</i>&nbsp;		}
<i>180</i>&nbsp;
<i>181</i>&nbsp;		public static &lt;T, E extends T&gt; String saveViaDataStore(DataStore&lt;T&gt; dataStore, E obj) {
<b class="nc"><i>182</i>&nbsp;			return String.valueOf(dataStore.save(obj));</b>
<i>183</i>&nbsp;		}
<i>184</i>&nbsp;
<i>185</i>&nbsp;		public static String saveViaConverter(ViennaContext ctx, Object obj) {
<b class="fc"><i>186</i>&nbsp;			return convert(ctx, obj, String.class);</b>
<i>187</i>&nbsp;		}
<i>188</i>&nbsp;	}
<i>189</i>&nbsp;
<i>190</i>&nbsp;
<i>191</i>&nbsp;	public static class DeserializationUtils {
<b class="nc"><i>192</i>&nbsp;		private DeserializationUtils() {</b>
<b class="nc"><i>193</i>&nbsp;		}</b>
<i>194</i>&nbsp;
<i>195</i>&nbsp;		@SuppressWarnings(&quot;unchecked&quot;)
<i>196</i>&nbsp;		public static Object load(ViennaContext ctx, Class clazz, String value) {
<b class="fc"><i>197</i>&nbsp;			if (clazz == null || clazz == ObjectUtils.Null.class) {</b>
<b class="nc"><i>198</i>&nbsp;				return null;</b>
<i>199</i>&nbsp;			}
<i>200</i>&nbsp;
<b class="fc"><i>201</i>&nbsp;			boolean hasConverter = hasConverter(ctx, String.class, clazz);</b>
<b class="fc"><i>202</i>&nbsp;			if (hasConverter) {</b>
<b class="fc"><i>203</i>&nbsp;				return loadViaConverter(ctx, clazz, value);</b>
<i>204</i>&nbsp;			} else {
<b class="nc"><i>205</i>&nbsp;				return loadViaDataStore(ctx, clazz, value);</b>
<i>206</i>&nbsp;			}
<i>207</i>&nbsp;		}
<i>208</i>&nbsp;
<i>209</i>&nbsp;		@SuppressWarnings(&quot;unchecked&quot;)
<i>210</i>&nbsp;		public static &lt;T&gt; T loadViaDataStore(ViennaContext ctx, Class&lt;T&gt; clazz, String value) {
<b class="nc"><i>211</i>&nbsp;			MetadataProvider metadataProvider = ctx.getMetadataProvider(clazz);</b>
<b class="nc"><i>212</i>&nbsp;			if (metadataProvider != null) {</b>
<b class="nc"><i>213</i>&nbsp;				String idName = metadataProvider.getIdentifierName(clazz);</b>
<b class="nc"><i>214</i>&nbsp;				if (idName != null) {</b>
<b class="nc"><i>215</i>&nbsp;					Field field = ReflectionUtils.findField(clazz, idName);</b>
<b class="nc"><i>216</i>&nbsp;					Object key = ViennaUtils.DeserializationUtils.load(ctx, field.getType(), value);</b>
<b class="nc"><i>217</i>&nbsp;					return ctx.getDataStore(clazz).load(clazz, key);</b>
<i>218</i>&nbsp;				}
<i>219</i>&nbsp;			}
<b class="nc"><i>220</i>&nbsp;			return ctx.getDataStore(clazz).load(clazz, value);</b>
<i>221</i>&nbsp;		}
<i>222</i>&nbsp;
<i>223</i>&nbsp;		public static &lt;T&gt; T loadViaConverter(ViennaContext ctx, Class&lt;T&gt; clazz, String value) {
<b class="fc"><i>224</i>&nbsp;			return convert(ctx, value, clazz);</b>
<i>225</i>&nbsp;		}
<i>226</i>&nbsp;	}
<i>227</i>&nbsp;}
</div>
</div>

<div class="footer">
    
    <div style="float:right;">generated on 2012-07-17 20:22</div>
</div>
</body>
</html>
